package auth

import (
	"io/ioutil"
	"net/http"
	"strconv"

	"sieve_admin_server_userpass/pkg/im3/app"
)

func ShowAuthPage(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, AuthPageUrl(), http.StatusFound)
}

func AuthPageUrl() string {
	return "https://" + app.Server() + "/auth/login?appid=" + strconv.Itoa(app.AppId())
}

func VerifyAuth(uid, sign string) bool {
	url := "https://" + app.Server() + "/auth/verify?appid=" + strconv.Itoa(app.AppId()) + "&uid=" + uid + "&sign=" + sign
	resp, err := http.Get(url)
	if err != nil {
		return false
	}
	defer resp.Body.Close()
	content, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return false
	}
	return string(content) == "OK"
}
